package com.etc.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.etc.entity.Album;
import com.etc.entity.Languages;
import com.etc.entity.Mood;
import com.etc.entity.MusicInfo;
import com.etc.entity.MusicLyric;
import com.etc.entity.MusicStyle;
import com.etc.entity.PageInfo;
import com.etc.entity.Scene;
import com.etc.entity.Singger;
import com.etc.entity.Theme;
import com.etc.service.MusicService;
import com.etc.service.MusicStyleService;
import com.etc.service.impl.MusicServiceImpl;
import com.etc.service.impl.MusicStyleServiceImpl;
import com.etc.util.SimpleUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**@author nxlnian
 * @author Amber
 * Servlet implementation class MusicServlet
 */
@WebServlet("/music")
@MultipartConfig
public class MusicServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MusicService musicService = new MusicServiceImpl();
	private MusicStyleService musicStyleService = new MusicStyleServiceImpl();

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if("music".equals(op)) {
			doMusic(request,response);
		}else if ("detail".equals(op)) {
			doFindSongDetailById(request, response);
		} else if ("deleteById".equals(op)) {
			doDeleteById(request, response);
		} else if ("add".equals(op)) {
			doAddSong(request, response);
		} else if ("pageByCondition".equals(op)) {
			doPageByCondition(request, response);
		} else if ("modify".equals(op)) {
			doModifySongInfo(request, response);
		} else if ("theme".equals(op)) {
			doGetTheme(request, response);
		} else if ("mood".equals(op)) {
			doGetMood(request, response);
		} else if ("scene".equals(op)) {
			doGetScene(request, response);
		} else if ("languages".equals(op)) {
			doGetLanguages(request, response);
		} else if ("userId".equals(op)) {
			doMusicByUserId(request,response);

		}else if ("musicList".equals(op)) {
			doMusicList(request,response);

		}else if ("songSleetId".equals(op)) {
			doMusicBySongSleetId(request,response);
		} else if ("export".equals(op)) {
			doExport(request, response);
		}else if ("search".equals(op)) {
			doSearch(request, response);
		}
	}

	private void doExport(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HSSFWorkbook wb = new HSSFWorkbook();
		//建立新的sheet对象（excel的表单）
		HSSFSheet sheet=wb.createSheet("歌曲表");
		List<String[]> list = new ArrayList<String[]>();
		String[] rows = new String[]{"歌曲编号","歌曲名","首字母","歌手名","专辑名","风格名","主题名","心情名","场景名","语种名","发布日期","上传日期","点赞数","播放量","歌曲海报图文件名"};  
        list.add(rows);
        for (int i = 0; i < list.size(); i++) {
        	HSSFRow row = null;
        	if(i==1){
        		row = sheet.createRow(2);
        	}else{
        		row = sheet.createRow(i);
        	}
        	String[] rowsName = list.get(i);
			for(int c = 0;c<rowsName.length;c++){
				HSSFCell cell = row.createCell(c);
				cell.setCellValue(rowsName[c]);
			}
		}
        List<Object[]>  dataList = new ArrayList<Object[]>();  
        Object[] objs = null;
        
        List<MusicInfo> mylist = musicService.exportMusicInfos();
        for (int i = 0; i < mylist .size(); i++) {  
        	MusicInfo musicInfo = mylist.get(i);  
            objs = new Object[rows.length];  
            objs[0] = musicInfo.getMusicId();
            objs[1] = musicInfo.getMusicName();
            objs[2] = musicInfo.getInitials();
            objs[3] = musicInfo.getSingger().getSinggerName();
            objs[4] = musicInfo.getAlbum().getAlbumName();
            objs[5] = musicInfo.getMusicStyle().getStyleName();
            objs[6] = musicInfo.getTheme().getThemeName();
            objs[7] = musicInfo.getMood().getMoodName();
            objs[8] = musicInfo.getScene().getSceneName();
            objs[9] = musicInfo.getLanguages().getLanguagesName();
            objs[10] = musicInfo.getReleaseTime();
            objs[11] = musicInfo.getCreateTim();
            objs[12] = musicInfo.getLikeNumber();
            objs[13] = musicInfo.getPlayVolume();
            objs[14] = musicInfo.getMusicImgPath();
            dataList.add(objs);  
        }
        for(int i = 0; i < dataList.size(); i++) {
        	HSSFRow row =sheet.createRow(i+1);
        	Object[] objsRow = dataList.get(i);
        	int length = 0;
        	for (int j = 0; j < objsRow.length; j++) {
				HSSFCell cell = row.createCell(j);
				if(objsRow[j]!=null && !objsRow[j].toString().isEmpty()){
					if(objsRow[j].toString().length()>length){
						//获取本列最高的字符长度
						length =  objsRow[j].toString().length();
					}
					cell.setCellValue(objsRow[j].toString());
				}
			}
		}
        OutputStream output=response.getOutputStream();
		response.reset();
		response.setHeader("Content-disposition", "attachment; filename=details.xls");
		response.setContentType("application/msexcel");
		wb.write(output);
		output.close();
	}

	private void doMusicBySongSleetId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String songSleetId = request.getParameter("songSleetId");
		List<MusicInfo> musicInfos = musicService.findBysongSleetId(Integer.parseInt(songSleetId));
		// 响应
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(musicInfos);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
	
	}

	private void doMusicByUserId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userId = request.getParameter("userId");
		List<MusicInfo> musicInfos = musicService.findByUserId(Integer.parseInt(userId));
		// 响应
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(musicInfos);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
	}
	
	private void doGetTheme(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/plain;charset=utf-8");
		List<Theme> list = musicStyleService.getThemeAll();
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(list);
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void doGetMood(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/plain;charset=utf-8");
		List<Mood> list = musicStyleService.getMoodsAll();
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(list);
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void doGetScene(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/plain;charset=utf-8");
		List<Scene> list = musicStyleService.getScenesAll();
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(list);
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void doGetLanguages(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/plain;charset=utf-8");
		
		List<Languages> list = musicStyleService.getLanguagesAll();
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(list);
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void doModifySongInfo(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		Integer musicId = Integer.parseInt(request.getParameter("musicId"));
		String musicName = request.getParameter("musicName");
		String singgerName = request.getParameter("singgerName");
		Integer singgerId = Integer.parseInt(request.getParameter("singgerId")); // 由主页面传参得到
		String albumName = request.getParameter("albumName");
		Integer albumId = Integer.parseInt(request.getParameter("albumId")); // 由主页面传参得到
		Integer styleId = Integer.parseInt(request.getParameter("musicStyle"));
		Integer themeId = Integer.parseInt(request.getParameter("theme"));
		Integer moodId = Integer.parseInt(request.getParameter("mood"));
		Integer sceneId = Integer.parseInt(request.getParameter("scene"));
		Integer languagesId = Integer.parseInt(request.getParameter("languages"));
		String releaseTime = request.getParameter("releaseTime");
		Part partLrc = request.getPart("musicLyric");// 歌词文件上传---lrc
		Part partImg = request.getPart("musicImgPath");// 歌曲海报上传，可设置默认图片---图片格式
		Part partMP3 = request.getPart("musicMp3Path");// 歌曲MP3文件上传---音频格式
		
		MusicInfo musicInfo = new MusicInfo();
		musicInfo.setMusicName(musicName);
		musicInfo.setMusicId(musicId);
		// 对首字母进行处理
		SimpleUtils obj = new SimpleUtils();
		String initials = obj.String2Alpha(musicName.substring(0,1));
		musicInfo.setInitials(initials);
		Singger singger =  new Singger();
		singger.setSinggerId(singgerId);
		singger.setSinggerName(singgerName); // 给定歌手名在业务层进行判断是否存在并处理得到歌手id
		musicInfo.setSingger(singger );
		Album album = new Album(); 
		album.setAlbumId(albumId);
		album.setAlbumName(albumName); // 同上（与歌手处理相同）
		musicInfo.setAlbum(album);
		MusicStyle musicStyle = new MusicStyle();  // 风格==下拉框进行选择
		musicStyle.setStyleId(styleId);
		// musicStyle.setStyleName(styleName);
		musicInfo.setMusicStyle(musicStyle);
		Theme theme = new Theme(); // 主题==下拉框进行选择
		theme.setThemeId(themeId);
		//theme.setThemeName(themeName);
		musicInfo.setTheme(theme);
		Mood mood = new Mood(); // 心情==下拉框进行选择
		mood.setMoodId(moodId);
		//mood.setMoodName(moodName);
		musicInfo.setMood(mood);
		Scene scene = new Scene(); // 场景==下拉框进行选择
		scene.setSceneId(sceneId);
		//scene.setSceneName(sceneName);
		musicInfo.setScene(scene);
		Languages languages = new Languages(); // 语种==下拉框进行选择
		languages.setLanguagesId(languagesId);
		//languages.setLanguagesName(languagesName);
		musicInfo.setLanguages(languages);
		musicInfo.setReleaseTime(releaseTime); // 日期控件
		
		// 歌词文件上传处理
		if (partLrc.getSize() != 0) {
			// 1.对上传的文件进行MIME类型判断
			String allowImgContentType = "application/x-digitalloca";
			// 获取上传文件的内容类型
			String fileContentType = partLrc.getContentType();
						
			if (allowImgContentType.indexOf(fileContentType) == -1) {
					// 响应结果文件类型不匹配
					out.print("文件类型不匹配,仅支持lrc!");
					return;
			}else {
				// 获取原文件名
				String realFileName  = partLrc.getSubmittedFileName();
				// 获取原文件名的拓展名
				String fileSuffix =  realFileName.substring(realFileName.lastIndexOf("."));
				// 存储文件名,保证文件名唯一
				StringBuilder fileName = new StringBuilder();
			    // 拼接方式: 歌曲名+文件扩展名
			    fileName.append(musicName).append(fileSuffix);
				// 关联文件存储的目录
				File file = new File("D:\\file\\lrc"); 
				// 定义文件路径
				StringBuilder filePath = new StringBuilder();
				filePath.append(file).append(File.separator).append(fileName );
				// 将文件写入指定位置
				partLrc.write(filePath.toString());
				// 歌曲对象设置图片路径属性,数据库存储仅是文件名
				MusicLyric musicLyric = new MusicLyric();
				musicLyric.setMusicLyrId(musicId);
				musicLyric.setMusicLyrName(fileName.toString());
				musicInfo.setMusicLyric(musicLyric);
			}
		}
		
		// 歌曲音频文件上传的格式处理
		if (partMP3.getSize() != 0) {
			// 1.对上传的文件进行MIME类型判断
			String allowImgContentType = "audio/mpeg,audio/midi,audio/ogg,audio/mp4,audio/wav,audio/x-ms-wma,audio/mp4a-latm";
			// 获取上传文件的内容类型
			String fileContentType = partMP3.getContentType();
						
			if (allowImgContentType.indexOf(fileContentType) == -1) {
					// 响应结果文件类型不匹配
					out.print("文件类型不匹配,支持mp3,mid,ogg,mp4a,wav,wma,m4a!");
					return;
			}else {
				// 获取原文件名
				String realFileName  = partMP3.getSubmittedFileName();
				// 获取原文件名的拓展名
				String fileSuffix =  realFileName.substring(realFileName.lastIndexOf("."));
				// 存储文件名,保证文件名唯一
				// 时间戳+4位随机数  或 时间戳+4位随机数+原文件名
				StringBuilder fileName = new StringBuilder();
			    // 拼接方式: 歌曲名+文件扩展名
			    fileName.append(musicName).append(fileSuffix);
				// 关联文件存储的目录
				File file = new File("D:\\file\\MP3"); 
				// 定义文件路径
				StringBuilder filePath = new StringBuilder();
				filePath.append(file).append(File.separator).append(fileName );
				// 将文件写入指定位置
				partMP3.write(filePath.toString());
				// 歌曲对象设置图片路径属性,数据库存储仅是文件名
				musicInfo.setMusicMp3Path(fileName.toString()); 
			}
		}
		
		// 对歌曲的海报文件上传进行处理
		if (partImg.getSize() != 0) {
			// 1.对上传的文件进行MIME类型判断
			String allowImgContentType = "image/jpg,image/jpeg,image/png,image/gif";
			// 获取上传文件的内容类型
			String fileContentType = partImg.getContentType();
						
			if (allowImgContentType.indexOf(fileContentType) == -1) {
					// 响应结果文件类型不匹配
					out.print("文件类型不匹配,支持image/jpg,image/jpeg,image/png,image/gif!");
					return;
			}else if(partImg.getSize() > 40000) {
					// 响应结果文件大小不匹配
					out.print("图片大小不能超过40K!");
					return;
			}else {
					// 获取原文件名
					String realFileName  = partImg.getSubmittedFileName();
					// 获取原文件名的拓展名
					String fileSuffix =  realFileName.substring(realFileName.lastIndexOf("."));
					// 存储文件名,保证文件名唯一
					// 时间戳+4位随机数  或 时间戳+4位随机数+原文件名
					StringBuilder fileName = new StringBuilder();
					// 时间戳
					LocalDateTime now = LocalDateTime.now();
					DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
					String time = dateTimeFormatter.format(now);
					// 4位随机数
					int random = (int) ((Math.random() * 9 + 1) * 1000); // 1000-9999
				    // 拼接方式1: 没有保留原文件名
				    fileName.append(time).append(random).append(fileSuffix);  // 202106101704125727.jpg
					// 关联文件存储的目录
					File file = new File("D:\\file\\img"); 
					// 定义文件路径
					StringBuilder filePath = new StringBuilder();
					filePath.append(file).append(File.separator).append(fileName);
					// 将文件写入指定位置
					partImg.write(filePath.toString());
					// 歌曲对象设置图片路径属性,数据库存储仅是文件名
					musicInfo.setMusicImgPath(fileName.toString());
			}
		}
		boolean flag = musicService.modifySongInfo(musicInfo);
		if (flag) {
			// 响应成功结果
			out.print("歌曲信息修改成功");
		}
		
	}

	private void doPageByCondition(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String musicName = request.getParameter("musicName");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		PageInfo<MusicInfo> pageInfo = musicService.getSongInfoBySongName(musicName,Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(pageInfo);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
	}

	private void doAddSong(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String musicName = request.getParameter("musicName");
		String singgerName = request.getParameter("singgerName");
		String albumName = request.getParameter("albumName");
		Integer styleId = Integer.parseInt(request.getParameter("musicStyle"));
		Integer themeId = Integer.parseInt(request.getParameter("theme"));
		Integer moodId = Integer.parseInt(request.getParameter("mood"));
		Integer sceneId = Integer.parseInt(request.getParameter("scene"));
		Integer languagesId = Integer.parseInt(request.getParameter("languages"));
		String releaseTime = request.getParameter("releaseTime");
		Part partLrc = request.getPart("musicLyric");// 歌词文件上传---lrc
		Part partImg = request.getPart("musicImgPath");// 歌曲海报上传，可设置默认图片---图片格式
		Part partMP3 = request.getPart("musicMp3Path");// 歌曲MP3文件上传---音频格式
		
		MusicInfo musicInfo = new MusicInfo();
		musicInfo.setMusicName(musicName);
		// musicInfo.setInitials(initials);  此处不需要处理，在业务层会自动处理
		Singger singger =  new Singger();
		// singger.setSinggerId(singgerId);
		singger.setSinggerName(singgerName); // 给定歌手名在业务层进行判断是否存在并处理得到歌手id
		musicInfo.setSingger(singger );
		Album album = new Album(); 
		// album.setAlbumId(albumId);
		album.setAlbumName(albumName); // 同上（与歌手处理相同）
		musicInfo.setAlbum(album);
		MusicStyle musicStyle = new MusicStyle();  // 风格==下拉框进行选择
		musicStyle.setStyleId(styleId);
		// musicStyle.setStyleName(styleName);
		musicInfo.setMusicStyle(musicStyle);
		Theme theme = new Theme(); // 主题==下拉框进行选择
		theme.setThemeId(themeId);
		//theme.setThemeName(themeName);
		musicInfo.setTheme(theme);
		Mood mood = new Mood(); // 心情==下拉框进行选择
		mood.setMoodId(moodId);
		//mood.setMoodName(moodName);
		musicInfo.setMood(mood);
		Scene scene = new Scene(); // 场景==下拉框进行选择
		scene.setSceneId(sceneId);
		//scene.setSceneName(sceneName);
		musicInfo.setScene(scene);
		Languages languages = new Languages(); // 语种==下拉框进行选择
		languages.setLanguagesId(languagesId);
		//languages.setLanguagesName(languagesName);
		musicInfo.setLanguages(languages);
		musicInfo.setReleaseTime(releaseTime); // 日期控件
		
		
		// 歌词文件上传处理
		if (partLrc.getSize() != 0) {
			
				// 获取原文件名
				String realFileName  = partLrc.getSubmittedFileName();
				// 获取原文件名的拓展名
				String fileSuffix =  realFileName.substring(realFileName.lastIndexOf("."));
				// 存储文件名,保证文件名唯一
				StringBuilder fileName = new StringBuilder();
			    // 拼接方式: 歌曲名+文件扩展名
			    fileName.append(musicName).append(fileSuffix);
				// 关联文件存储的目录
				File file = new File("D:\\file\\lrc"); 
				// 定义文件路径
				StringBuilder filePath = new StringBuilder();
				filePath.append(file).append(File.separator).append(fileName );
				// 将文件写入指定位置
				partLrc.write(filePath.toString());
				// 歌曲对象设置图片路径属性,数据库存储仅是文件名
				MusicLyric musicLyric = new MusicLyric();
				musicLyric.setMusicLyrName(fileName.toString());
				musicInfo.setMusicLyric(musicLyric);
			
		}
		
		// 歌曲音频文件上传的格式处理
		if (partMP3.getSize() != 0) {
			// 1.对上传的文件进行MIME类型判断
			String allowImgContentType = "audio/mpeg,audio/midi,audio/ogg,audio/mp4,audio/wav,audio/x-ms-wma,audio/mp4a-latm";
			// 获取上传文件的内容类型
			String fileContentType = partMP3.getContentType();
						
			if (allowImgContentType.indexOf(fileContentType) == -1) {
					// 响应结果文件类型不匹配
					out.print("文件类型不匹配,支持mp3,mid,ogg,mp4a,wav,wma,m4a!");
					return;
			}else {
				// 获取原文件名
				String realFileName  = partMP3.getSubmittedFileName();
				// 获取原文件名的拓展名
				String fileSuffix =  realFileName.substring(realFileName.lastIndexOf("."));
				// 存储文件名,保证文件名唯一
				// 时间戳+4位随机数  或 时间戳+4位随机数+原文件名
				StringBuilder fileName = new StringBuilder();
			    // 拼接方式: 歌曲名+文件扩展名
			    fileName.append(musicName).append(fileSuffix);
				// 关联文件存储的目录
				File file = new File("D:\\file\\MP3"); 
				// 定义文件路径
				StringBuilder filePath = new StringBuilder();
				filePath.append(file).append(File.separator).append(fileName );
				// 将文件写入指定位置
				partMP3.write(filePath.toString());
				// 歌曲对象设置图片路径属性,数据库存储仅是文件名
				musicInfo.setMusicMp3Path(fileName.toString()); 
			}
		}
		
		// 对歌曲的海报文件上传进行处理
		if (partImg.getSize() != 0) {
			// 1.对上传的文件进行MIME类型判断
			String allowImgContentType = "image/jpg,image/jpeg,image/png,image/gif";
			// 获取上传文件的内容类型
			String fileContentType = partImg.getContentType();
						
			if (allowImgContentType.indexOf(fileContentType) == -1) {
					// 响应结果文件类型不匹配
					out.print("文件类型不匹配,支持image/jpg,image/jpeg,image/png,image/gif!");
					return;
			}else if(partImg.getSize() > 40000) {
					// 响应结果文件大小不匹配
					out.print("图片大小不能超过40K!");
					return;
			}else {
					// 获取原文件名
					String realFileName  = partImg.getSubmittedFileName();
					// 获取原文件名的拓展名
					String fileSuffix =  realFileName.substring(realFileName.lastIndexOf("."));
					// 存储文件名,保证文件名唯一
					// 时间戳+4位随机数  或 时间戳+4位随机数+原文件名
					StringBuilder fileName = new StringBuilder();
					// 时间戳
					LocalDateTime now = LocalDateTime.now();
					DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
					String time = dateTimeFormatter.format(now);
					// 4位随机数
					int random = (int) ((Math.random() * 9 + 1) * 1000); // 1000-9999
				    // 拼接方式1: 没有保留原文件名
				    fileName.append(time).append(random).append(fileSuffix);  // 202106101704125727.jpg
					// 关联文件存储的目录
					File file = new File("D:\\file\\img"); 
					// 定义文件路径
					StringBuilder filePath = new StringBuilder();
					filePath.append(file).append(File.separator).append(fileName);
					// 将文件写入指定位置
					partImg.write(filePath.toString());
					// 歌曲对象设置图片路径属性,数据库存储仅是文件名
					musicInfo.setMusicImgPath(fileName.toString());
			}
		}else {
				musicInfo.setMusicImgPath("default-goods-img.jpg"); // 设置默认图
		}
		boolean flag = musicService.addSong(musicInfo);
		if (flag) {
			// 响应成功结果
			out.print("歌曲添加成功");
		}
	}

	private void doDeleteById(HttpServletRequest request, HttpServletResponse response) throws IOException {

		Integer musicId = Integer.parseInt(request.getParameter("musicId"));
		boolean flag = musicService.deleteSongById(musicId);
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(flag);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		
	}

	private void doFindSongDetailById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer musicId = Integer.parseInt(request.getParameter("musicId"));
		MusicInfo musicInfo = musicService.getMusicInfoById(musicId);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(musicInfo);
		out.print(json);
	}

	private void doMusic(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String musicId = request.getParameter("musicId");
		MusicInfo MusicInfo = musicService.findByid(Integer.parseInt(musicId));
		// 响应
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(MusicInfo);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
	}
	private void doMusicList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 接收请求参数
				String singgerId = request.getParameter("singgerId");
				
				String pageNum = request.getParameter("pageNum");
				int pageSize = 10;
				// 调用业务层
				PageInfo<MusicInfo> pageInfo = musicService.getsongListBySingerId(Integer.parseInt(singgerId),Integer.parseInt(pageNum), pageSize);
				
				// 响应
				// 利用第三方库jackson将Java对象转换成JSON格式
				ObjectMapper objectMapper = new ObjectMapper();
				String json = objectMapper.writeValueAsString(pageInfo);
				response.setContentType("application/json;charset=utf-8");
				PrintWriter out = response.getWriter();
				out.write(json);
				
	}
	private void doSearch(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		// 接收请求参数
				String keyWord = request.getParameter("keyWord");
				
				String pageNum = request.getParameter("pageNum");
				int pageSize = 10;
				// 调用业务层
				PageInfo<MusicInfo> pageInfo = musicService.getSongListByKeyWord(keyWord, Integer.parseInt(pageNum), pageSize);
				
				// 响应
				// 利用第三方库jackson将Java对象转换成JSON格式
				ObjectMapper objectMapper = new ObjectMapper();
				String json = objectMapper.writeValueAsString(pageInfo);
				response.setContentType("application/json;charset=utf-8");
				PrintWriter out = response.getWriter();
				out.write(json);
				
	}
}
